package com.rdc.qingkou.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rdc.qingkou.R;
import com.rdc.qingkou.entity.SubscribeBean;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


public class SubscribeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private final String TAG = "SUBSCRIBE_TEST";

    private final static int HAS_MESSAGE = 0;
    private final static int NO_MESSAGE = 1;
    private List<SubscribeBean> mSubscribeList;
    private OnSubscribeRecyclerViewListener onSubscribeRecyclerViewListener;

    public interface OnSubscribeRecyclerViewListener{
        void onItemClick(int position);
        void onSubscribe(int position);
    }

    public void setOnSubscribeRecyclerViewListener(OnSubscribeRecyclerViewListener onSubscribeRecyclerViewListener){
        this.onSubscribeRecyclerViewListener=onSubscribeRecyclerViewListener;
    }

    public SubscribeRecyclerViewAdapter(Context context, List<SubscribeBean> mSubscribeList) {
//    public SubscribeRecyclerViewAdapter(Context context, List<SubscribeBean> mSubscribeList , Integer hasMessage) {
        this.mSubscribeList = mSubscribeList;
//        this.hasMessage = hasMessage;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view;
        if (viewType == HAS_MESSAGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_subscribe_recyclerview, parent, false);
            return new SubscribeViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_subscribe_none, parent, false);
            return new NoneViewHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        SubscribeViewHolder holder=(SubscribeViewHolder)viewHolder;
        holder.position=position;

        SubscribeBean message = mSubscribeList.get(position);
        final SubscribeViewHolder subscribeViewHolder = (SubscribeViewHolder) holder;
        setSubscribeData(holder, message);
    }

    @Override
    public int getItemCount() {
        return mSubscribeList.size();
    }

    //给itemView加载数据
    private void setSubscribeData(final SubscribeRecyclerViewAdapter.SubscribeViewHolder holder, SubscribeBean subscribeBean) {


        Uri uri1 = Uri.parse(""+subscribeBean.getHeadSculpturePath());
        Uri uri2 = Uri.parse("res://drawable/" + subscribeBean.getAddSubscribe());
        if (subscribeBean.getName() != null){
            holder.name.setText(subscribeBean.getName().replace("\"",""));
        }
        if (subscribeBean.getContent() != null){
            holder.content.setText(subscribeBean.getContent().replace("\"",""));
        }

        holder.addSubscribe.setImageURI(uri2);

        Log.d(TAG, "URI 1 : " + subscribeBean.getHeadSculpturePath());

        Glide.with(holder.itemView.getContext()).load(uri1).placeholder(R.drawable.imageview_default).dontAnimate().into(holder.headSculpture);
    }


    public class SubscribeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener{

        public View rootView;
        public int position;
        public CircleImageView headSculpture;
        public TextView name;
        public TextView content;
        public ImageView addSubscribe;
        public SubscribeBean subscribeBean;

        SubscribeViewHolder(View itemView) {
            super(itemView);

            headSculpture = (CircleImageView) itemView.findViewById(R.id.item_subscribe_headSculpture_simpledraweeView);

            name = (TextView) itemView.findViewById(R.id.item_subscribe_name_textView);
            content = (TextView) itemView.findViewById(R.id.item_subscribe_content_textView);
            addSubscribe = (ImageView) itemView.findViewById(R.id.item_subscribe_addSubscribe_imageView);
            addSubscribe.setOnClickListener(this);
            rootView=itemView.findViewById(R.id.item_subscribe_view);
            rootView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {

            if (v.equals(addSubscribe)){
                onSubscribeRecyclerViewListener.onSubscribe(position);
            }else if(null!=onSubscribeRecyclerViewListener){
                onSubscribeRecyclerViewListener.onItemClick(position);
            }

        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    }

    private static class NoneViewHolder extends RecyclerView.ViewHolder {
        NoneViewHolder(View itemView) {
            super(itemView);
        }
    }
}
